package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqcpMachineproduct;
import com.jy.adscm.operation.mscyb.bean.JqcpMachineproductdetail;
import com.jy.adscm.operation.mscyb.bean.JqdbMachinepacking;
import com.jy.adscm.operation.mscyb.bean.JqwbMachinemaintenance;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqcpMachineproductMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqcpMachineproductdetailMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqwbMachinemaintenanceMapper;
import com.jy.adscm.operation.mscyb.service.JqcpMachineproductService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-07
 */
@Service
public class JqcpMachineproductServiceImpl extends ServiceImpl<JqcpMachineproductMapper, JqcpMachineproduct> implements JqcpMachineproductService {
     @Autowired
    JyCodeMapper jyCodeMapper;
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JqcpMachineproductdetailMapper jqcpMachineproductdetailMapper;
     @Autowired
    JqwbMachinemaintenanceMapper jqwbMachinemaintenanceMapper;
     @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;

    @Override
    public Boolean updateJqcpMachineproduct(JqcpMachineproduct jqcpMachineproduct, Token token) throws AdscmSystemException{
        //待办修改
        if (jqcpMachineproduct.getTaskstate()==0 && jqcpMachineproduct.getTaskfinishstate()==0 &&
                "0".equals(jqcpMachineproduct.getIstaskstart()) &&  "0".equals(jqcpMachineproduct.getIstaskbegin()) &&
                "0".equals(jqcpMachineproduct.getIstaskend())  && "0".equals(jqcpMachineproduct.getIsreviewtask())
        ){
            jqcpMachineproduct.setTaskreleaseperson(token.getUser().getId().toString());
            jqcpMachineproduct.setTaskreleaseaddtime(new Date());

            return  super.updateById(jqcpMachineproduct);
        }
        //接收任务
        if (jqcpMachineproduct.getTaskstate()==0 && jqcpMachineproduct.getTaskfinishstate()==0 &&
                "1".equals(jqcpMachineproduct.getIstaskstart()) &&  "0".equals(jqcpMachineproduct.getIstaskbegin()) &&
                "0".equals(jqcpMachineproduct.getIstaskend())  && "0".equals(jqcpMachineproduct.getIsreviewtask())
        ){
            jqcpMachineproduct.setIstaskstart("1");
            jqcpMachineproduct.setTaskstartaddtime(new Date());
            jqcpMachineproduct.setTaskstate(1);
            return  super.updateById(jqcpMachineproduct);
        }
        //开始任务
        if (jqcpMachineproduct.getTaskstate()==1 && jqcpMachineproduct.getTaskfinishstate()==0 &&
                "1".equals(jqcpMachineproduct.getIstaskstart()) &&  "1".equals(jqcpMachineproduct.getIstaskbegin()) &&
                "0".equals(jqcpMachineproduct.getIstaskend())  && "0".equals(jqcpMachineproduct.getIsreviewtask())
        ){
            jqcpMachineproduct.setIstaskbegin("1");
            jqcpMachineproduct.setTaskbeginaddtime(new Date());
            return super.updateById(jqcpMachineproduct);
        }
        //结束任务
        if (jqcpMachineproduct.getTaskstate()==1 && jqcpMachineproduct.getTaskfinishstate()==0 &&
                "1".equals(jqcpMachineproduct.getIstaskstart()) &&  "1".equals(jqcpMachineproduct.getIstaskbegin()) &&
                "1".equals(jqcpMachineproduct.getIstaskend())  && "0".equals(jqcpMachineproduct.getIsreviewtask())
        ){
            jqcpMachineproduct.setIstaskend("1");
            jqcpMachineproduct.setTaskfinishstate(1);
            jqcpMachineproduct.setTaskendaddtime(new Date());
            //生成维保记录
            Wrapper<JqcpMachineproductdetail> wrapper =new EntityWrapper<>();
            wrapper.eq("TID",jqcpMachineproduct.getTid());
            List<JqcpMachineproductdetail> jqcpMachineproductdetails = jqcpMachineproductdetailMapper.selectList(wrapper);
            if (jqcpMachineproductdetails.size()>0){
                JqcpMachineproductdetail jqcpMachineproductdetail = jqcpMachineproductdetails.get(0);
                JqwbMachinemaintenance jqwbMachinemaintenance =new JqwbMachinemaintenance();
                jqwbMachinemaintenance.setUserdeptname(jqcpMachineproductdetail.getCustomer());
                jqwbMachinemaintenance.setTaskcode(jqcpMachineproduct.getTaskcode());
                jqwbMachinemaintenanceMapper.insert(jqwbMachinemaintenance);

            }else {
              throw  new AdscmSystemException("没找到出库信息");
            }
            return super.updateById(jqcpMachineproduct);
        }
        //复核任务
        if (jqcpMachineproduct.getTaskstate()==1 && jqcpMachineproduct.getTaskfinishstate()==1 &&
                "1".equals(jqcpMachineproduct.getIstaskstart()) &&  "1".equals(jqcpMachineproduct.getIstaskbegin()) &&
                "1".equals(jqcpMachineproduct.getIstaskend())  && "1".equals(jqcpMachineproduct.getIsreviewtask())
        ){
            jqcpMachineproduct.setIsreviewtask("1");
            jqcpMachineproduct.setReviewaddtime(new Date());
            jqcpMachineproduct.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqcpMachineproduct);
        }

        return null;
    }

    @Override
    public List<JqcpMachineproduct> selectJqcpMachineproduct(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<JqcpMachineproduct> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<JqcpMachineproduct> jqcpMachineproducts = super.selectList(wrapper);

        for (JqcpMachineproduct jqcpMachineproduct : jqcpMachineproducts) {
            if ( jqcpMachineproduct.getTaskreleaseperson()!=null && !"".equals( jqcpMachineproduct.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcpMachineproduct.getTaskreleaseperson());
                jqcpMachineproduct.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqcpMachineproduct.getTaskperformperson()!=null && !"".equals( jqcpMachineproduct.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcpMachineproduct.getTaskperformperson());
                jqcpMachineproduct.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqcpMachineproduct.getReviewperson()!=null && !"".equals( jqcpMachineproduct.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqcpMachineproduct.getReviewperson());
                jqcpMachineproduct.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcpMachineproduct.getResourcename(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqcpMachineproduct.getResourcename());
                jqcpMachineproduct.setResourcenameName(jyDrawingType.getName());
            }


        }
        return jqcpMachineproducts;

    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("99");
        return jyUsers;
    }
}
